// pages/my/my.js

const db = wx.cloud.database()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        user: {},
        SignIn_index: '0',
        appointment_index: '0',
        userInfo: '',
        Violation_index: 0,
        cancel_index:0,
        Appointment_info:''
    },
    //跳转到登录页面
    login() {
        wx.navigateTo({
            url: '../register/register',
        })
    },
    //退出登录
    loginOut() {
        this.setData({ user: null })
        wx.setStorageSync('user', null)
    },
    //跳转到我的预约
    my_appointment() {
        wx.navigateTo({
            url: '../my_appointment/my_appointment',
        })
    },
    //跳转到我的举报
    my_report() {
        wx.navigateTo({
            url: '../my_report/my_report',
        })
    },
    //跳转到我的借阅
    my_Borrowing() {
        wx.navigateTo({
            url: '../my_Borrowing/my_Borrowing',
        })
    },
    //跳转到我的收藏
    my_Collection() {
        wx.navigateTo({
            url: '../my_Collection/my_Collection',
        })
    },
    //跳转到备忘录
    memorandum() {
        wx.navigateTo({
            url: '../memorandum/memorandum',
        })
    },
    //跳转到个人资料
    PersonalData() {
        wx.navigateTo({
            url: '../PersonalData/PersonalData',
        })
    },
    //跳转到用户须知
    Instruc_users() {
        wx.navigateTo({
            url: '../Instruc_users/Instruc_users',
        })
    },
    //跳转到关于我们
    AboutUs() {
        wx.navigateTo({
            url: '../AboutUs/AboutUs',
        })
    },
    //获取用户数据
    getuserList() {
        let that = this
        let user = wx.getStorageSync('user')
        let openid = user._openid
        db.collection("user").where({ _openid: openid }).get({
            success(res) {
                that.setData({ userInfo: res.data })
            }
        })
    },
    //获取用户详细数据
    getuserdeail() {
        db.collection("userInfo").where({ _openid: this.data.userInfo._openid }).orderBy('_updateTime', 'desc').get().then(res => {
            this.setData({
                Appointment_info: res.data
            })
            var SignIn_index = 0
            var appointment_index = 0
            var Violation_index = 0
            var cancel_index=0
            for (let i = 0; i < res.data.length; i++) {
                if (res.data[i].check_state == 1) {
                    SignIn_index++
                }
                if (res.data[i].seat_state == 4) {
                    Violation_index++
                }
                if (res.data[i].seat_state == 3) {
                    cancel_index++
                }
                appointment_index++
            }
            this.setData({
                SignIn_index: SignIn_index,
                appointment_index: appointment_index,
                Violation_index: Violation_index
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getuserdeail()
        this.getuserList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let user = wx.getStorageSync('user')
        console.log('缓存的user', user)
        this.setData({
            user: user,
        })
        this.getuserList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})